package com.study.it.xw.controller;

import com.study.it.xw.dao.AttentionDao;
import com.study.it.xw.entity.Attention;
import com.study.it.xw.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class AttentionController {
    @Autowired
    private AttentionDao dao;
    //查询关注列表（当前登录用户关注）
    @GetMapping("att/List")
    public String showAttList(Model m, HttpSession session){
        User u=(User)session.getAttribute("user");
        m.addAttribute("list",dao.selectByUser(u.getId()));
        return "attention";
    }

    //添加关注
    @RequestMapping("/att/add")
    public String addAtt(int marstid,HttpSession session){
        User u=(User)session.getAttribute("user");
        int c=dao.selectHasAtt(u.getId(),marstid);
        if(c==0){
            Attention att=new Attention();
            att.setUserId(u.getId());
            att.setMarstId(marstid);
            dao.insertAtt(att);
        }
        return "redirect:/att/List";
    }

    //取消关注
    @RequestMapping("/att/cancel")
    public String cancelAtt(int attid){
        dao.deleteAtt(attid);
        return "redirect:/att/List";
    }
}
